//
//  ViewController.m
//  ZLShang
//
//  Created by wangguangming on 15/7/23.
//  Copyright © 2015年 Amber. All rights reserved.
//

#import "ViewController.h"
#import "Rootview.h"
#import "Nrootview.h"
#import "Loadviewcontroller.h"
#import "UserModel.h"
#import "Loadsingle.h"
#import "Ordersignle.h"
#import "ReadyDiSignle.h"
#import "WDViewController.h"  //待配货
#import "RDViewController.h"
#import "CancelViewController.h"
#import "ProblemViewController.h"
#import "WaitSendViewController.h"
#import "SendViewController.h"
#import "Base.h"
#import "SearchViewController.h"
#import "AttendanceViewController.h"
#import "MineViewController.h"
#import "WorkerViewController.h"
#import "statisticalViewController.h"
#import "NdoeliveryViewController.h"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    BOOL isLoad;
    
    NSInteger _role;//角色
    
    BOOL _isShit;
    NSArray *_itemArray;
    
    UIView *othersView;
}
//@property(nonatomic,strong)Rootview *root;
@property(nonatomic,strong)Nrootview *root;
@end

@implementation ViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) 
        {
       self.root=[[Nrootview alloc] initWithFrame:[UIScreen mainScreen].bounds];
        }
    return self;
}
-(void)loadView
{
    self.view=self.root;
    self.root.menuTable.delegate=self;
    self.root.menuTable.dataSource=self;
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
        // NSLog(@"%@",[Loadsingle single].userArray);
    UserModel *userModel=[Loadsingle single].userArray[0];
    
    
        //  NSLog(@"%@",userModel);
    
    self.title=@"首页";
    

    
    _role =userModel.role.integerValue;
    
    
    if (_role==1)
        {
         [othersView removeFromSuperview];
        _itemArray=@[@"首页",@"出勤管理",@"人员管理",@"数据统计",@"个人中心",@"未完成包裹",@"注销"];
       }
    else if (_role==2)
        {
         [othersView removeFromSuperview];
          _itemArray=@[@"首页",@"出勤管理",@"人员管理",@"数据统计",@"个人中心",@"未完成包裹",@"注销"];
        }
    else if (_role==3)//送
        {
         [othersView removeFromSuperview];
        _itemArray=@[@"首页",@"数据统计",@"个人中心",@"注销"];
        othersView=[[UIView alloc] initWithFrame:CGRectMake(0, 64, WIDTH, HEIGHT-64)] ;
        othersView.backgroundColor=[UIColor whiteColor];

        [self songhuo];
        }
    else if (_role==4)//配
        {
        [othersView removeFromSuperview];
         _itemArray=@[@"首页",@"数据统计",@"个人中心",@"注销"];
        othersView=[[UIView alloc] initWithFrame:CGRectMake(0, 64, WIDTH, HEIGHT-64)] ;
        othersView.backgroundColor=[UIColor whiteColor];        

        [self peihuo];
        }
    else if (_role==5)//配送
        {
         [othersView removeFromSuperview];
        _itemArray=@[@"首页",@"数据统计",@"个人中心",@"注销"];
        
        }

}
-(void)peihuo//配货
{
    NSArray *array=@[@"待配货",@"已配货",@"已取消"];
    for (int i=0; i<3; i++)
        {
        UIButton *button=[[UIButton alloc] initWithFrame:CGRectMake(5, 5+(5+(WIDTH-10)/3)*i, WIDTH-10, (WIDTH-10)/3)];
        
        [button setTitle:array[i] forState:UIControlStateNormal];
        button.backgroundColor=[UIColor redColor];
        [button setTag:110+i];
        [button addTarget:self action:@selector(chooseAction:) forControlEvents:UIControlEventTouchUpInside];
        othersView.userInteractionEnabled=YES;
        [othersView addSubview:button];                                                         
       }
    
}
-(void)songhuo//送货
{
    NSArray *array=@[@"领取订单",@"我的发货记录",@"我的问题件"];
    for (int i=0; i<3; i++)
        {
        for (int i=0; i<3; i++)
            {
            UIButton *button=[[UIButton alloc] initWithFrame:CGRectMake(5, 5+(10+(WIDTH-10)/3)*i, WIDTH-10, (WIDTH-10)/3)];
            
            [button setTitle:array[i] forState:UIControlStateNormal];
             [button setTag:1100+i];
             button.backgroundColor=[UIColor redColor];
            [button addTarget:self action:@selector(chooseAction:) forControlEvents:UIControlEventTouchUpInside];
            othersView.userInteractionEnabled=YES;
             [othersView addSubview:button]; 
             
            
            }

        }
}
- (void)viewDidLoad {
    [super viewDidLoad];
      
    _isShit=NO;
    
    UIBarButtonItem *left=[[UIBarButtonItem alloc] initWithTitle:@"在路上" style:UIBarButtonItemStylePlain target:self action:@selector(itemAction)];
    self.navigationItem.leftBarButtonItem=left;
    UIBarButtonItem *right=[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"searchios.png"] style:UIBarButtonItemStylePlain target:self action:@selector(searchAction)];
    self.navigationItem.rightBarButtonItem=right;

     if (!isLoad)
        {
        Loadviewcontroller * modalView = [[Loadviewcontroller alloc]init];
        modalView.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
        
        
        [self presentViewController:modalView animated:NO completion:nil];
        } 
       
    for (int i=0; i<6; i++)
        {
        UIButton *button=(UIButton *)[self.root viewWithTag:110+i];
        [button addTarget:self action:@selector(chooseAction:) forControlEvents:UIControlEventTouchUpInside];
       }
    
    
  

//    [self.root.waitDistributions addTarget:self action:@selector(turnAction:) forControlEvents:UIControlEventTouchUpInside];
//    [self.root.distributions addTarget:self action:@selector(turnAction:) forControlEvents:UIControlEventTouchUpInside];
//    [self.root.sends addTarget:self action:@selector(turnAction:) forControlEvents:UIControlEventTouchUpInside];
//    [self.root.waitSends addTarget:self action:@selector(turnAction:) forControlEvents:UIControlEventTouchUpInside];
//    [self.root.cancels addTarget:self action:@selector(turnAction:) forControlEvents:UIControlEventTouchUpInside];
//    [self.root.problem addTarget:self action:@selector(turnAction:) forControlEvents:UIControlEventTouchUpInside];
    
   
    
    
}
-(void)searchAction
{
    SearchViewController *searchViewController=[[SearchViewController alloc] init];
    [self.navigationController pushViewController:searchViewController animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

  
-(void)handleColorChange
{
    Loadviewcontroller * modalView = [[Loadviewcontroller alloc]init];
    modalView.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    
    
    [self presentViewController:modalView animated:NO completion:nil];

}
-(void)chooseAction:(UIButton *)sender
{
    if (sender.tag==110)
        {
        WDViewController *wdViewController=[[WDViewController alloc] init];
        wdViewController.title=@"未配货订单";
        [self.navigationController pushViewController:wdViewController animated:YES];
       }
    else if(sender.tag==112)
        {
        UserModel *userModel=[Loadsingle single].userArray[0];
        
       
//        [[ReadyDiSignle signle] getRdItemWorking:userModel._id];
//      
        RDViewController *rdviewController=[[RDViewController alloc] init];
        rdviewController.title=@"已配货订单";
        [self.navigationController pushViewController:rdviewController animated:YES];

        }
    else if(sender.tag==113)
        {
        [[ReadyDiSignle signle] getCancelWorking];
        CancelViewController *cancelViewController=[[CancelViewController alloc] init];
        cancelViewController.title=@"已取消订单";
        [self.navigationController pushViewController:cancelViewController animated:YES];
        }
    else if(sender.tag==111)
        {
        [[ReadyDiSignle signle] getNeedSendworking];
        
        WaitSendViewController *waitSendViewController=[[WaitSendViewController alloc] init];
        waitSendViewController.title=@"未发货订单";
        [self.navigationController pushViewController:waitSendViewController animated:YES];
        }
    else if(sender.tag==114)
        {
        
//      [[ReadyDiSignle signle] getSendworking];
        
        SendViewController *sendViewController=[[SendViewController alloc] init];
        sendViewController.title=@"发货记录";
        [self.navigationController pushViewController:sendViewController animated:YES];
        
     
        
        }
    else if(sender.tag==115)
        {
        [[ReadyDiSignle signle] getNotSaveworking];
        [[ReadyDiSignle signle] getSaveworking];
        ProblemViewController *problemViewController=[[ProblemViewController alloc] init];
        problemViewController.title=@"问题件";
        [self.navigationController pushViewController:problemViewController animated:YES];
        }
        
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(_role==1|_role==2)
        {
        return 8;
        }
    else
        {
        return 5;
        }
    
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    UITableViewCell *cell=[[UITableViewCell alloc] init]
    ;
    if (_role==1|_role==2)
        {
        if (indexPath.row<7)
            {
            UILabel *laber=[[UILabel alloc] initWithFrame:CGRectMake(10, 5, WIDTH*2/3-20, 30)];
            laber.text=_itemArray[indexPath.row];
            [cell addSubview:laber];
            
            }
        }
    else
        {
        if (indexPath.row<4)
            {
            
            UILabel *laber=[[UILabel alloc] initWithFrame:CGRectMake(10, 5, WIDTH*2/3-20, 30)];
            laber.text=_itemArray[indexPath.row];
            [cell addSubview:laber];
            
            }
        
        
        }
    return cell;
}
-(void)tableView:(nonnull UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath
{
    [self itemAction];
    if(_role==3|_role==4)
        {
        if (indexPath.row==3)
            {
            NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
            
            [[Loadsingle single] loagout];
            
            [nc addObserver:self
                   selector:@selector(handleColorChange)
                       name:@"donot"
                     object:nil];
            
            
            
            }
        else if(indexPath.row==2)
            {
            MineViewController *mineViewController=[[MineViewController alloc] init];
            [self.navigationController pushViewController:mineViewController animated:YES];
            }
        else if(indexPath.row==1)
            {
            statisticalViewController *statistical=[[statisticalViewController alloc] init];
            [self.navigationController pushViewController:statistical animated:YES];
            
            }
        
        else 
            {
            
            }
        
        
        }
    else
        
        {
        
        if (indexPath.row==6)
            {
            NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
            
            [[Loadsingle single] loagout];
            
            [nc addObserver:self
                   selector:@selector(handleColorChange)
                       name:@"donot"
                     object:nil];
            }
        else if(indexPath.row==1)
            {
            
            AttendanceViewController *attendanceViewController=[[AttendanceViewController alloc] init];
            [self.navigationController pushViewController:attendanceViewController animated:YES];
            }
        else if(indexPath.row==2)
            {
            UserModel *userModel=[Loadsingle single].userArray[0];
            
            
            WorkerViewController *attendanceViewController=[[WorkerViewController alloc] init];
            attendanceViewController.mktidString=userModel.mktId;
            [self.navigationController pushViewController:attendanceViewController animated:YES];
            }
        else if(indexPath.row==4)
            {
            
            MineViewController *mineViewController=[[MineViewController alloc] init];
            [self.navigationController pushViewController:mineViewController animated:YES];
            }
        else if(indexPath.row==3)
            {
            statisticalViewController *statistical=[[statisticalViewController alloc] init];
            [self.navigationController pushViewController:statistical animated:YES];
            
            }
        else if(indexPath.row==5)
            {
            NdoeliveryViewController *ndoeliveryViewController=[[NdoeliveryViewController alloc] init];
            [self.navigationController pushViewController:ndoeliveryViewController animated:YES];
            
            }
        
        
        }
    
}
-(void)itemAction
{
    if (!_isShit)
        {
        [UIView animateWithDuration:0.5 animations:^{
            self.root.menuTable.frame=CGRectMake(0, 64, WIDTH/2, HEIGHT-64);
        }];
        
        _isShit=YES; 
        }
    else
        {
        [UIView animateWithDuration:0.5 animations:^{
            self.root.menuTable.frame=CGRectMake(0, 64, 0, HEIGHT-64);
        }];
        
        _isShit=NO;
        }
    
}

@end
